<template>
    <div class="receiveFile-page page-wrapper ">
        <div >
            {{BrowserMemSize.used}} / {{BrowserMemSize.total}}

            <a :href="fileHref">
                文件链接
            </a>
        </div>
    </div>
</template>

<script>
    import Flier from "@/utils/filer.js"
    import { GetFileSize,checkQuota } from "@/utils/index.js"
    import { mapMutations,mapGetters } from "vuex";
    let filer = new Flier({});
    export default {
        name: "receive-file",
        data:function(){
            return {
                BrowserMemSize:{
                    used:"",
                    total:"",
                },
                sendUser:"",
                myId:"",
                fileHref:""
            }
        },
        methods:{
            //初始化 WS
            init(){
                let id = 1

                filer.on('task', function(task) {
                    addTask(task);
                });
                filer.on('progress', ({fileID, progress, fileName, fileURL})=>{
                    if(progress === 1 && fileURL){
                        console.log(fileURL,this.url);
                        this.fileHref = fileURL;
                        this.$electron.ipcRenderer.send("download",{
                            url:fileURL,
                            directory:this.url,
                            id:id++
                        });
                    }
                });
                filer.on('status', function({fileID, status}){
                    console.log(status);
                });
                filer.on('error', function(err){ // you need to use switch/cases to take action on each type of errors
                    console.log('err: ', err);
                });
                function addTask(task){
                    console.log(task);
                }
                this.ws = new WebSocket('ws://localhost:8443');
                this.ws.onopen = evt => {
                    filer.signalingChannel = this.ws;
                    console.log('webSocket connected');
                };
                this.ws.onmessage = msg => {
                    try {
                        var msgObj = JSON.parse(msg.data);
                    } catch (e){
                        console.log('Oops, unknown msg: ', e);
                        return
                    }
                    console.log('new msg from ws server: ', msgObj);
                    switch (msgObj.msgType) {
                        case "newUser":

                            break;

                        case "removeUser":

                            break;

                        case "profile":
                            this.sendUser = msgObj.peersID[0];
                            this.myId = msgObj.userID;
                            filer.myID = msgObj.userID;
                            this.ws.send(JSON.stringify({
                                msgType:"file",
                                data:{
                                    fileId:msgObj.userID,
                                    fileName:"文件处理"
                                }
                            }));
                            break;

                        case "signaling":
                            filer.handleSignaling(msgObj);
                            break;

                        default: console.log('Oops. unknown msg: ', msgObj)
                    }
                };
            },
        },
        mounted(){
            // this.init();
            checkQuota().then(Rs =>{
                console.log(Rs)
            })
        },
        computed:{
            url(){
                return this.$store.state.config.url
            },
        },
    }
</script>

<style lang="less">
    .receiveFile-page{

    }
</style>
